var machineGuid = null;
jQuery(document).ready(function ($) {

    machineGuid = getCookie('machineGuid');
    if(machineGuid == null || machineGuid == ''){
        machineGuid = WebUploader.Base.guid();
        setCookie('machineGuid', machineGuid, 40000);
    }

    $(".nav-ul-div").click(function (event) {
        if ($(this).next().is(':visible') == false) {
            $(".nav-ul ul").slideUp();
        }

        $('.nav-ul-div>i').not($(this).find('.toggle-i')).addClass('fa-chevron-right').removeClass('fa-chevron-down');
        $(this).find('.toggle-i').toggleClass('fa-chevron-right fa-chevron-down');

        $(this).next().slideToggle();

        $('.nav-ul-div').removeClass('activecolor');
        $(this).addClass('activecolor');

        $('.div-inner-ul li').removeClass('inner-active');

        //设置主界面为none
        // $('#comment-relate').css('display', 'none');
    });

    $(".div-inner-ul li").click(function (event) {
        $('.div-inner-ul li').removeClass('inner-active');
        $(this).addClass('inner-active');
    });

    /*
     *关联js
     */

    $(".div-inner-ul li").click(function (event) {
        var id = $(this).attr("id");
        var path = "page/" + id + ".html";
        console.log(path);
        setBread(id);
        $.ajax({
            url: path,
            cache: false,
            success: function (html) {
                $("#comment-relate").html(html);
            }
        });
        // $.get("../page/relate.html",function(data){  
        //     $("#comment-relate").html(data);//初始化加载界面  
        // }); 
    });

    //点击主界面显示

    //面包屑Js  
    function setBread(id) {
        $('#getbread').html('');
        id = "#" + id;
        first = $(id).parent().siblings('.nav-ul-div').find('a').text();
        second = $(id).text();
        $('#getbread').html('<li>' +
            '<a href="#">' + first + '</a>' +
            '<span class="divider"></span></li>' +
            '<li><a href="#">' + second + '</a>' +
            '<span class="divider"></span></li>');
    }

});

function setCookie(c_name, value, expiredays) {
    // var exdate = new Date();
    // exdate.setDate(exdate.getDate() + expiredays);
    // document.cookie = c_name + "=" + escape(value) +
    //     ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    $.cookie(c_name, value, {expries: expiredays});
}

function getCookie(c_name) {
    return $.cookie(c_name);
        
    // if (document.cookie.length > 0) {
    //     c_start = document.cookie.indexOf(c_name + "=");
    //     if (c_start != -1) {
    //         c_start = c_start + c_name.length + 1;
    //         c_end = document.cookie.indexOf(";", c_start);
    //         if (c_end == -1) c_end = document.cookie.length;
    //         return unescape(document.cookie.substring(c_start, c_end));
    //     }
    // }
    // return "";
}

function checkIsRunState(cookieNamePrefix, runHandler){
    var state = getCookie(cookieNamePrefix + 'state');
    if(state != null && state == 'run'){
        runHandler(getCookie(cookieNamePrefix + 'guid'));
    }
}

function setRunState(cookieNamePrefix, isRun, guid, clickFunName){
    setCookie(cookieNamePrefix + 'state', isRun ? 'run' : 'noRun', 7);
    if(isRun){
        setCookie(cookieNamePrefix + 'guid', guid, 7);
    }
}

function initHistoryDropDownMenu(cookieNamePrefix, menuId, clickFunName){
    var history = getCookie(cookieNamePrefix + 'history');
    if(history == null)
        return;
    var splits = history.split('&');
    if(!splits.length)
        return;

    var historyMenu = $(menuId).find('ul');
    if(!historyMenu.length){
        historyMenu = $(menuId).append('<ul class="dropdown-menu"></ul>').find('ul');
    }
    for(var index in splits){
        var guid = splits[index].split('(')[0];
        historyMenu.append('<li><a href="#" value="' + guid + '" onclick="' + clickFunName + '(this)">' + splits[index] + '</a></li>');
    }
}

function addItemToHistory(cookieNamePrefix, menuId, guid, date, clickFunName){
    var cookieName = cookieNamePrefix + 'history';
    var history = getCookie(cookieName);
    if(history == null)
        history = '';
    else
        history += '&';
    var addValue = guid + '(' + date.toLocaleString() + ')';
    history += addValue;
    setCookie(cookieName, history, 1);

    var historyMenu = $(menuId).find('ul');
    if(!historyMenu.length){
        historyMenu = $(menuId).append('<ul class="dropdown-menu"></ul>').find('ul');
    }
    historyMenu.append('<li><a href="#" value="' + guid + '" onclick="' + clickFunName + '(this)">' + addValue + '</a></li>');
}